package com.loie.xcx.service.impl;

import com.loie.xcx.comm.CustomException;
import com.loie.xcx.mapper.QuotationMapper;
import com.loie.xcx.model.bo.QuotationBO;
import com.loie.xcx.model.bo.QuotationLineBO;
import com.loie.xcx.model.dto.QuotationLineQueryDTO;
import com.loie.xcx.model.dto.QuotationQueryDTO;
import com.loie.xcx.service.QuotationService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

/**
 * @author zhl
 */
@Service
@AllArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class QuotationServiceImpl implements QuotationService {
    final QuotationMapper quotationMapper;

    @Override
    public List<QuotationBO> getQuotationList(QuotationQueryDTO dto) {
        return quotationMapper.getQuotationList(dto);
    }

    @Override
    public int getQuotationListTotal(QuotationQueryDTO dto) {
        return quotationMapper.getQuotationListTotal(dto);
    }

    @Override
    public QuotationBO getQuotation(QuotationQueryDTO dto) {
        return quotationMapper.getQuotation(dto);
    }

    @Override
    public List<QuotationLineBO> getQuotationLineList(QuotationQueryDTO quotationQueryDTO) {
        return quotationMapper.getQuotationLineList(quotationQueryDTO);
    }

    @Override
    public int getQuotationLineListTotal(QuotationQueryDTO dto) {
        return quotationMapper.getQuotationLineListTotal(dto);
    }

    @Override
    public QuotationLineBO getQuotationLine(QuotationLineQueryDTO dto) {
        return quotationMapper.getQuotationLine(dto);
    }

    @Override
    public QuotationLineBO getQuotationLinePhoto(QuotationLineQueryDTO dto) {
        return quotationMapper.getQuotationLinePhoto(dto);
    }


    @Override
    public String addQuotation(QuotationBO quotationBO, List<QuotationLineBO> list) {
        String parentId = UUID.randomUUID().toString().toUpperCase();
        quotationBO.setRecordId(parentId);
        quotationMapper.insertQuotation(quotationBO);
        for (QuotationLineBO quotationLineBO : list) {
            quotationLineBO.setParentId(parentId);
            quotationLineBO.setRecordId(UUID.randomUUID().toString().toUpperCase());
            quotationMapper.insertQuotationLine(quotationLineBO);
        }
        return parentId;
    }

    @Override
    public void updateQuotation(QuotationBO quotationBO, List<QuotationLineBO> list) {
        int ret = quotationMapper.updateQuotation(quotationBO);
        if (ret != 1) {
            throw new CustomException(400, "Unexpected updateQuotation: " + ret);
        }
        String parentId = quotationBO.getRecordId();
        quotationMapper.deleteQuotationLineAll(parentId);
        for (QuotationLineBO quotationLineBO : list) {
            quotationLineBO.setParentId(parentId);
            quotationLineBO.setRecordId(UUID.randomUUID().toString().toUpperCase());
            quotationMapper.insertQuotationLine(quotationLineBO);
        }
    }

    @Override
    public void deleteQuotation(QuotationBO quotationBO) {
        // 该状态
        int ret = quotationMapper.deleteQuotation(quotationBO);
        if (ret != 1) {
            throw new CustomException(400, "Unexpected deleteQuotation: " + ret);
        }
        //  不删了 改主表状态就行了
//        quotationMapper.deleteQuotationLineAll(quotationBO.getRecordId());
    }
}
